package cn.bjxy.shishaolong.ra_room.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.ra_room.entity.RaRomDeleteRequest;
import cn.bjxy.shishaolong.ra_room.entity.RaroomStudentRalation;
import cn.bjxy.shishaolong.ra_room.service.IRaroomRequestRalationService;
import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.request_answer.service.IAnswerService;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 答疑室提问相关控制器
 *
 * @Author 时少龙
 * @Date 2019-04-20 14:02
 * @Version 1.0
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/web/raroom/request")
public class RaRoomRequestController extends RaroomBaseController {

    @Autowired
    private IRaroomRequestRalationService requestRalationService;

    @Autowired
    private IAnswerService answerService;

    /**
     * 查询答疑室问题  RoomId 为必填字段
     *
     * @return
     */
    @Permission
    @RequestMapping("/queryByCondition")
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            if (!param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                return retParamFailure();
            }
            Request myRequest = param.getObject(SysConst.QUERY_CONDITION_KEY, Request.class);
            if (myRequest.getRoomId() == null) return retParamFailure();

//            Integer currentUserId = getCurrentUserId(request);

            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Request> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Request> list = requestRalationService.queryRequestForRoomByCondition(page, myRequest);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Request> list = requestRalationService.queryRequestForRoomByCondition(myRequest);
                return ResultMsg.success(list);
            }
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return retQueryFailure();
        }
    }

    /**
     * 提问
     *
     * @param myRequest
     * @return
     */
    @PostMapping("/request")
    @Permission
    public ResultMsg request(HttpServletRequest request, @RequestBody Request myRequest) {
        try {
            if (myRequest == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            Integer currentUserId = getCurrentUserId(request);
            myRequest.setUserId(currentUserId);
            checkStudentInRoom(currentUserId, myRequest.getRoomId()); // 判断学生是否在答疑室中

            requestRalationService.request(myRequest);
            return ResultMsg.success();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return ResultMsg.create(ofe.getCode(), ofe.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 回答
     *
     * @param answer
     * @return
     */
    @PostMapping("/answer")
    @Permission
    public ResultMsg answer(HttpServletRequest request, @RequestBody Answer answer) {
        try {
            if (answer == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            Integer currentUserId = getCurrentUserId(request);
            answer.setUserId(currentUserId).setPid(-1);
            answerService.saveOrUpdate(answer);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 删除 问答
     *
     * @param deleteRequest
     * @return
     */
    @DeleteMapping("/delete")
    @Permission
    public ResultMsg delete(HttpServletRequest httRequest, @RequestBody RaRomDeleteRequest deleteRequest) {
        try {
            if (deleteRequest == null) {
                return retParamFailure();
            }
            Integer userId = getCurrentUserId(httRequest);
            if (!isRoomOpen(userId)) retRoomStatusNotOpen();
            if (!isRoomTeacher(userId, deleteRequest.getRoomId())) retNotOwnOperatorFailure(); // 非答疑室教师本人操作
            // 设置roomId
            Integer roomId = getRoomIdByTeacherId(userId);
            deleteRequest.setRoomId(roomId);
            // 删除
            requestRalationService.remove(deleteRequest);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
        }
    }


}
